#ifndef _INCLUDE_KEY_H
#define _INCLUDE_KEY_H
#include <stdint.h>

/********************Key Configuration START********************/

/* Continuous pressed and long pressed can be both enabled.
** But in fact we just use one of them.
**/
#define KEY_CONT_PRESSED_ENABLE
#define KEY_LONG_PRESSED_ENABLE

/* The report period for continuous pressed. */
#define KEY_CONT_PRESSED_TIME 500//500ms
/* The time to determine a long pressed. */
#define KEY_LONG_PRESSED_TIME 2000//2s
/* The delay to avoid shaking */
#define KEY_SHAKE_TIME        20//20ms
/* The maximum number of supported key */
#define KEY_MAX_NUM       8
/* The size of key event buffer */
#define KEY_EVT_BUF_SIZE  1

/********************Key Configuration END********************/

#define KEY_EVT_INVALID        0x00//Invalid key.
#define KEY_EVT_PRESSED        0x01//Key is pressed.
#define KEY_EVT_CONT_PRESSED   0x02//Key is pressed continuously.
#define KEY_EVT_LONG_PRESSED   0x04//Key is pressed for a long time.
#define KEY_EVT_RELEASED       0x08//Key is released

typedef struct key_evt
{
    int32_t id;
    int32_t key;
    int32_t evt;
}key_evt_t;

typedef int32_t (*key_read_t)(int32_t key);
typedef void (*key_cb_t)(key_evt_t event);

/* USAGE
1. Call key_init() to initilise key component.
2. Call key_register() to tell system key infomation.
3. Call key_detect() periodicly(ex. every 10ms) to detect key. The key event will be kept in buffer.
4. Call key_event_xxx() periodicly to get and process key event.
PS:
1. key_handle() is a builtin function to get and process key event.
   It will get all key event and call the callback.
   It is convenient than calling key_event_xxx() by yourself.
2. Call key_task_init() will initialise a builtin task to call key_detect() and key_handle().
*/

//Get key event from bufer
int32_t key_event_get(key_evt_t *event);
int32_t key_event_get_num(void);
void key_event_clear(void);

void key_init(void);
int32_t key_register(int32_t key, int32_t level, key_read_t read, key_cb_t cb);
void key_detect(uint32_t ts);


void key_handle(void);

void key_task_init(void);

#endif //_INCLUDE_KEY_H
